<?php
//require_once APPLICATION_PATH.'/library/QQMailer.php';
$serv = new swoole_server("127.0.0.1", 9501);

//设置异步任务的工作进程数量
$serv->set(array('task_worker_num' => 4));

//监听数据接收事件
$serv->on('receive', function($serv, $fd, $from_id, $data) {
    //投递异步任务
    $task_id = $serv->task($data);//非阻塞
    echo "同步代码执行完成 id= $task_id";
});

//处理异步任务
$serv->on('task', function ($serv, $task_id, $from_id, $data) {
    handleFun($data);
    //返回任务执行的结果
    $serv->finish("finish");
});

//处理异步任务的结果
$serv->on('finish', function ($serv, $task_id, $data) {
    echo "异步任务执行完成";
});

$serv->start();

function handleFun($data){
    //$mailer = new \Application\QQMailer\QQMailer();\
    sleep(15);
    $value = [];
    $arr = json_decode($data,true);
    foreach ($arr as $value) {
        $str = $value['to'].'--'.$value['title'].'--'.$value['content'].'<br>';
        $path = "log.log";
        $str = "[".date("Y-m-d H:i:s")."]".$str;
        $str .= PHP_EOL;
        file_put_contents($path,$str,FILE_APPEND);
    }
}